package com.taiyo.modules.api.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.taiyo.common.utils.PageUtils;
import com.taiyo.common.utils.Query;
import com.taiyo.common.utils.R;
import com.taiyo.common.utils.RedisUtils;
import com.taiyo.common.validator.Assert;
import com.taiyo.modules.api.annotation.AuthIgnore;
import com.taiyo.modules.api.entity.TFeedbackEntity;
import com.taiyo.modules.api.service.TFeedbackService;
import com.taiyo.modules.sys.controller.AbstractController;
import com.taiyo.modules.sys.dao.SysUserDao;
import com.taiyo.modules.sys.entity.SysUserEntity;
import com.taiyo.modules.sys.service.SysUserService;




/**
 * 系统用户
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-02-04 17:11:40
 */
@RestController
@RequestMapping("/api/sysuser")
public class ApiUserController extends AbstractController{
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	private RedisUtils redisUtil;
	@Autowired
	private TFeedbackService tFeedbackService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<SysUserEntity> sysUserList = sysUserService.queryList(query);
		int total = sysUserService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(sysUserList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{userId}")
	public R info(@PathVariable("userId") Long userId){
		SysUserEntity sysUserEntity=sysUserService.queryObject(userId);
		
		return R.ok().put("sysUser", sysUserEntity);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody SysUserEntity sysUser){
		sysUserService.save(sysUser);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestParam Map<String, Object> params){
		System.out.print(params);
		String userInfo= (String) params.get("user");
		SysUserEntity sysUser= JSON.parseObject(userInfo,SysUserEntity.class);
//		SysUserEntity user1 = sysUserService.queryByUserName1(sysUser.getUsername());
//		// 用户名存在
//		if (user1 != null && user1.getUserId()!=sysUser.getUserId()) {
//			return R.error("用户名已存在");
//		}
//		SysUserEntity user2=sysUserService.queryByMobile(sysUser.getMobile());
//		if(user2!=null){
//			return R.error("手机号码已存在");
//		}
		sysUserDao.update(sysUser);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] userIds){
		sysUserService.deleteBatch(userIds);
		
		return R.ok();
	}
	
	/**
	 * 认证信息提交
	 */
	@AuthIgnore
	@RequestMapping("/auth")
	public R auth(@RequestParam Map<String, Object> params){
		String userId = params.get("userId") == null ? "" : params.get("userId").toString();
		String card = params.get("card") == null ? "" : params.get("card").toString();
		Assert.isBlank(userId, "用户id不能为空");
		Assert.isBlank(card, "身份证号码不能为空");
		
		SysUserEntity user = sysUserService.queryObject(Long.parseLong(userId));
		user.setCard(card);
		user.setAuth(1L);
		sysUserDao.update(user);
		return R.ok().put("msg", "认证成功");
	}
	
	// 找回密码
	@AuthIgnore
	@RequestMapping("/password")
	public R password(String mobile,String code, String newPassword) {
		Assert.isBlank(mobile, "手机号码不能为空");
		Assert.isBlank(code, "验证码不为能空");
		Assert.isBlank(newPassword, "新密码不为能空");
		SysUserEntity user = sysUserService.queryByMobile(mobile);

		if(user==null){
			return R.error("该手机未注册");
		}

		String codeRedis = redisUtil.get(user.getMobile());
		if (null ==codeRedis || !codeRedis.equalsIgnoreCase(code)) {
			return R.error("验证码不正确");
		}
		
		// sha256加密
		newPassword = new Sha256Hash(newPassword, user.getSalt()).toHex();
		user.setPassword(newPassword);
		sysUserDao.update(user);
		return R.ok();
	}

	// 用户反馈
	@AuthIgnore
	@RequestMapping("/feedback")
	public R feedback(String userId,String content){
		TFeedbackEntity tFeedback = new TFeedbackEntity();
		tFeedback.setContent(content);
		tFeedback.setUserId(Integer.parseInt(userId));
		tFeedback.setCreateTime(new Date());
		tFeedbackService.save(tFeedback);
		return R.ok();
	}
	
	/**
	 * 根据账号查询用户信息
	 */
	/**
	 * 用户信息
	 */
	@AuthIgnore
	@RequestMapping("/queryByAccount/{account}")
	public R info(@PathVariable("account") String account){
		SysUserEntity user = sysUserService.queryByAccount(account);
		return R.ok().put("user", user);
	}
}
